package net.lzzy.practices.web.course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.service.ChapterService;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 渊何
 */
@Api(tags = "章节相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_CHAPTER_ROOT)
public class ChapterApiController {
    private final ChapterService service;

    @Autowired
    public ChapterApiController(ChapterService service) {
        this.service = service;
    }

    @ApiOperation(value = "教师获取课程的章节", notes = "1.token(String)<br/>2.id(UUId)<br/>")
    @GetMapping(ApiConstants.ROUTE_CHAPTER_TEACHER)
    public ApiResult getTeachersChapters(@RequestParam String token, @RequestParam(name = "id") UUID courseId) {
        List<Chapter> chapters = service.getTeachersChapters(courseId, token);
        return transform(chapters);
    }

    private ApiResult transform(List<Chapter> chapters) {
        if (chapters == null) {
            return new ApiResult(false, Messages.WRONG_ID + "或" + Messages.NO_PERMISSION, null);
        }
        List<VmChapter> vmChapters = new ArrayList<>();
        chapters.forEach(chapter -> vmChapters.add(new VmChapter(chapter)));
        return new ApiResult(true, vmChapters.size() + "", vmChapters);
    }

    @ApiOperation(value = "学生获取课程的章节", notes = "1.token(String)<br/>2.id(UUId)<br/>")
    @GetMapping(ApiConstants.ROUTE_CHAPTER_STUDENT)
    public ApiResult getStudentsChapters(@RequestParam String token, @RequestParam(name = "id") UUID courseId) {
        List<Chapter> chapters = service.getStudentsChapters(courseId, token);
        return transform(chapters);
    }

    private ApiResult addOrUpdate(UUID id, ChapterInfo info) {
        String token = info.getToken();
        String name = info.getName();
        String desc = info.getDesc();
        String strCourseId = info.getCourseId();
        boolean open = info.isOpen();
        if (StringUtils.isEmpty(token) ||
                StringUtils.isEmpty(name) ||
                StringUtils.isEmpty(desc) ||
                StringUtils.isEmpty(strCourseId)) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        boolean create = id == null;
        UUID courseId = UUID.fromString(strCourseId);
        Chapter chapter = new Chapter();
        chapter.setOpen(open);
        chapter.setName(name);
        chapter.setDescription(desc);
        VmChapter vmChapter;
        if (create) {
            vmChapter = new VmChapter(service.add(courseId, chapter, token));
        } else {
            vmChapter = new VmChapter(service.update(id, chapter, token));
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), vmChapter);
    }

    @ApiOperation(value = "添加章节", notes = "1.token(String)<br/>2.id(UUId)<br/>")
    @PostMapping(ApiConstants.ROUTE_CHAPTER_ADD)
    public ApiResult add(@RequestBody ChapterInfo info) {
        return addOrUpdate(null, info);
    }

    @ApiOperation(value = "更新章节", notes = "1.token(String)<br/>2.id(UUId)<br/>")
    @PostMapping(ApiConstants.ROUTE_CHAPTER_UPDATE)
    public ApiResult update(@PathVariable UUID id, @RequestBody ChapterInfo info) {
        return addOrUpdate(id, info);
    }

    @ApiOperation(value = "是否开放章节", notes = "1.token(String)<br/>2.strChapterId(String)<br/>3.open(boolean)<br/>")
    @PostMapping(ApiConstants.ROUTE_CHAPTER_OPEN_CLOSE)
    public ApiResult openCloseChapter(@RequestBody ChapterOpenInfo info) {
        String token = info.getToken();
        String strChapterId = info.getChapterId();
        boolean open = info.isOpen();
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(strChapterId)) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Chapter chapter = service.openCloseChapter(UUID.fromString(strChapterId), open, token);
        if (chapter == null) {
            return new ApiResult(false, Messages.WRONG_ID + "或" + Messages.NO_PERMISSION, null);
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), new VmChapter(chapter));
    }

    @ApiOperation(value = "删除章节", notes = "1.token(String)<br/>2.chapterId(String)<br/>")
    @DeleteMapping(ApiConstants.ROUTE_CHAPTER_REMOVE)
    public ApiResult delete(@RequestBody ChapterKeyInfo info) {
        String token = info.getToken();
        String strChapterId = info.getChapterId();
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(strChapterId)) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Messages msg = service.remove(UUID.fromString(strChapterId), token);
        return new ApiResult(msg == Messages.SUCCESS, msg.toString(), null);
    }
}
